import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/routeHelper.dart';
import '../../model/cleancar.dart';
import '../../pages/homex/login.dart';
import '../../widgets/inputWidget.dart';

import '../../helper/channelHelper.dart';

import '../../helper/uidata.dart';
import 'package:dio/dio.dart';

import 'cleanobjects_list.dart';

class CleanCarLoginPage extends StatefulWidget {
  @override
  CleanCarLoginPageState createState() => new CleanCarLoginPageState();
}

class CleanCarLoginPageState extends State<CleanCarLoginPage> {
  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final key = new GlobalKey<ScaffoldState>();
  late List<CleanCar> carList;
  bool ifLoaded = false;
  late CleanCar carIndex;

  final lineBorder = OutlineInputBorder(
    /*边角*/
    borderRadius: BorderRadius.all(
      Radius.circular(8.0), //边角为30
    ),
    borderSide: BorderSide(
      color: Colors.blue, //边线颜色为黄色
      width: 1, //边线宽度为2
    ),
  );

  @override
  void initState() {
    super.initState();
    loadData();
  }

  loadData() async {
    carList = await CleanCar.GetAllList();
    ifLoaded = true;
    this.setState(() => {});
  }

  List<DropdownMenuItem> generateItemList() {
    List<DropdownMenuItem> items = [];
    for (var a in carList) {
      var item1 = new DropdownMenuItem(
          key: Key(a.CarCode), value: a, child: new Text(a.CarCode));
      items.add(item1);
    }
    return items;
  }

  getContain(Widget c1) {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.only(top: 16.0, left: 16.0, right: 16.0),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      child: c1,
    );
  }

  getSelectButton() {
    return DropdownButtonHideUnderline(
      child: DropdownButton(
        hint: Text('               '),
        //设置这个value之后,选中对应位置的item，
        //再次呼出下拉菜单，会自动定位item位置在当前按钮显示的位置处
        // style: TextStyle(color: Colors.white),
        value: carIndex,
        items: generateItemList(),
        onChanged: (u1) {
          setState(() {
            carIndex = u1;
            c1.text = carIndex.UserName;
            c2.text = carIndex.Password;
            this.setState(() => {});
          });
        },
      ),
    );
  }

  getButton() {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 0.0, horizontal: 30.0),
      width: double.infinity,
      margin: EdgeInsets.only(top: 12.0),
      child: TextButton(
        child: Text(
          "登录",
          style: TextStyle(color: Colors.white, fontSize: 16.0),
        ),
        // color: Colors.blue,
        onPressed: () {
          // getUser(c1.text, c2.text);
          carIndex.UserName = c1.text;
          carIndex.Password = c2.text;
          UIData.cleanCar = carIndex;
          RouteHelper.ShowPage(context, CleanObjectsListPage(true));
        },
      ),
    );
  }

  getHeader() {
    return Container(
      margin: EdgeInsets.only(bottom: 48.0),
      child: Text(
        UIData.appName,
        style: TextStyle(
            fontWeight: FontWeight.w400, color: Colors.white, fontSize: 24.0),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //backgroundColor: Colors.white,
        key: key,
        body: ifLoaded == false
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : Container(
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage("lib/assets/images/bg.png"),
                    fit: BoxFit.cover,
                  ),
                  color: Colors.transparent,
                ),
                child: Center(
                  child: loginFields(context),
                ),
              ));
  }

  loginBody(BuildContext context) => SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[loginFields(context)],
        ),
      );

  Widget getInput2(String title, TextEditingController c1, {double w1 = 120}) {
    return ListTile(
        title: Text(
          title,
          // style: TextStyle(fontWeight: FontWeight.bold),
        ),
        trailing: Container(
            width: w1,
            child: TextField(
              maxLines: 1,
              controller: c1,
              textAlign: TextAlign.center,
            )));
  }

  loginFields(BuildContext context) => SingleChildScrollView(
        child: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              getHeader(),
              getContain(
                  ListTile(title: Text("选择车辆"), trailing: getSelectButton())),
              getContain(getInput2("姓名", c1)),
              getContain(getInput2("联系电话", c2)),
              //getInput("用户名", c1,false),
              //getInput("密码", c2,true),
              SizedBox(
                height: 30.0,
              ),
              getButton(),
              SizedBox(
                height: 25.0,
              ),

              ElevatedButton(
                child: Text(
                  "管理员登录",
                  style: TextStyle(color: Colors.grey),
                ),
                onPressed: () => RouteHelper.ShowPage(context, LoginPage()),
              )
            ],
          ),
          margin: EdgeInsets.all(12.0),
        ),
      );
}
